package com.application.lucky.widget.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.application.lucky.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MoneyHistoryDialog extends BaseBottomEnterDialog {
    private TabLayout mTabView;
    private RecyclerView mRclView;
    private TextView mTvCancel;
    private MyAdapter myAdapter;
    List<String> redps;
    List<String> games;
    private Select select;

    /**
     * <item>全部</item>
     * <item>红包</item>
     * <item>提现</item>
     * <item>充值</item>
     * <item>佣金</item>
     * <item>转账</item>
     */


    public MoneyHistoryDialog(@NonNull Context context, Select select) {
        super(context);
        this.select = select;

    }

    @Override
    protected void initViews(Context context) {
        redps = Arrays.asList(context.getResources().getStringArray(R.array.select_money_type));
        games = Arrays.asList(context.getResources().getStringArray(R.array.select_games_type));


        mTabView = findViewById(R.id.tab_view);
        mTabView.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                myAdapter.clearSelect();
                if (tab.getPosition() == 0) {
                    myAdapter.setNewData(redps);
                } else {
                    myAdapter.setNewData(games);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mRclView = findViewById(R.id.rcl_view);
        mRclView.setLayoutManager(new GridLayoutManager(context, 3));
        myAdapter = new MyAdapter(false);
        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                myAdapter.setSelectNum(position);
                String item = myAdapter.getItem(position);
                if (games.contains(item)) {
                    if (select != null) {
                        select.select(item, -1);
                    }
                } else {
                    if (select != null) {
                        select.select(item, position);
                    }
                }


            }
        });
        mRclView.setAdapter(myAdapter);
        myAdapter.setNewData(redps);

        mTvCancel = findViewById(R.id.tv_cancel);
    }

    public interface Select {
        void select(String s, int position);
    }

    @Override
    protected int layoutResId() {
        return R.layout.dialog_money_history_view;
    }

    static class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        List<String> selectNum = new ArrayList<>();

        public int getSelectNum() {
            return selectNum.size();
        }

        public void clearSelect() {
            selectNum.clear();
            notifyDataSetChanged();
        }

        private boolean isSelectNUm;

        public MyAdapter(boolean isSelect) {
            super(R.layout.item_group_send_rp_button);
            this.isSelectNUm = isSelect;
        }

        public boolean isSelect(int s) {
            return selectNum.contains(s + "");
        }

        public String getSelect() {
            Collections.sort(selectNum);
            StringBuffer sb = new StringBuffer();
            for (String s : selectNum) {
                sb.append(Integer.valueOf(s) + 1).append(",");

            }
            String s = sb.toString();
            String substring = s.substring(0, s.length() - 1);

            return substring;
        }

        public void setSelectNum(int position) {

            if (isSelectNUm) {
                if (selectNum.contains(position + "")) {
                    selectNum.remove(position + "");
                } else {
                    if (selectNum.size() == 9) {
                        return;
                    }
                    selectNum.add(position + "");
                }
                notifyItemChanged(position);
            } else {
                selectNum.clear();
                selectNum.add(position + "");
                notifyDataSetChanged();
            }

        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            boolean contains = selectNum.contains(helper.getAdapterPosition() + "");
            helper.setText(R.id.tv_num_view, item).
                    setTextColor(R.id.tv_num_view, contains ? Color.parseColor("#ffffff") : Color.parseColor("#111111")).
                    setBackgroundRes(R.id.rl_view, contains ? R.drawable.shape_domain_text_cor_4 : R.drawable.shape_white_cor_4);
        }
    }

}
